package cn.edu.tju.elm.model;

import cn.edu.tju.core.model.BaseEntity;
import cn.edu.tju.core.model.User;
import jakarta.persistence.*;

@Entity
@Table(name = "delivery_address")
public class DeliveryAddress extends BaseEntity {
    
    @Column(name = "contact_name", nullable = false)
    private String contactName;
    
    @Column(name = "contact_sex", nullable = false)
    private Integer contactSex; // 1:男, 2:女
    
    @Column(name = "contact_tel", nullable = false)
    private String contactTel;
    
    @Column(name = "address", nullable = false, columnDefinition = "TEXT")
    private String address;
    
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "customer_id", nullable = false)
    private User customer;
    
    @Column(name = "is_default")
    private Boolean isDefault = false;
    
    // 构造方法
    public DeliveryAddress() {
    }
    
    public DeliveryAddress(String contactName, Integer contactSex, String contactTel, String address, User customer) {
        this.contactName = contactName;
        setContactSex(contactSex); // 使用setter进行验证
        this.contactTel = contactTel;
        this.address = address;
        this.customer = customer;
    }
    
    // getters and setters
    public String getContactName() {
        return contactName;
    }
    
    public void setContactName(String contactName) {
        this.contactName = contactName;
    }
    
    public Integer getContactSex() {
        return contactSex;
    }
    
    public void setContactSex(Integer contactSex) {
        // 验证性别只能是1(男)或2(女)
        if (contactSex != null && contactSex != 1 && contactSex != 2) {
            throw new IllegalArgumentException("性别只能是1(男)或2(女)");
        }
        this.contactSex = contactSex;
    }
    
    public String getContactTel() {
        return contactTel;
    }
    
    public void setContactTel(String contactTel) {
        this.contactTel = contactTel;
    }
    
    public String getAddress() {
        return address;
    }
    
    public void setAddress(String address) {
        this.address = address;
    }
    
    public User getCustomer() {
        return customer;
    }
    
    public void setCustomer(User customer) {
        this.customer = customer;
    }
    
    public Boolean getIsDefault() {
        return isDefault;
    }
    
    public void setIsDefault(Boolean isDefault) {
        this.isDefault = isDefault;
    }
    
    // 辅助方法
    public String getContactSexDisplay() {
        if (contactSex == null) {
            return "未知";
        } else if (contactSex == 1) {
            return "男";
        } else if (contactSex == 2) {
            return "女";
        }
        return "未知";
    }
    
    @Override
    public String toString() {
        return "DeliveryAddress{" +
                "id=" + getId() +
                ", contactName='" + contactName + '\'' +
                ", contactSex=" + contactSex +
                ", contactTel='" + contactTel + '\'' +
                ", address='" + address + '\'' +
                ", customer=" + (customer != null ? customer.getId() : "null") +
                ", isDefault=" + isDefault +
                '}';
    }
}
